<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Announce extends BaseModel
{

    protected $table = 'announce';
    public $timestamps = false;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 获取首页资讯数据
     */
    public function getIndexAnnouce($config){
         $data = [];
         foreach ($config as $c){
             $where['type'] = $c['type'];
             $Announce = $this->getList($where,'','',$c['limit']);
             if ($Announce){
                 $data[] = $Announce->toArray();
             }
         }
         return $data;
    }

    /**
     * 获取所有公告列表
     */
    public static function getAnnounceList($pagesize){
        $data = static ::paginate($pagesize);
        return $data;
    }

    /**
     * 修改公告
     */
    public function eidtannounce($req){
        $announce = $this->find($req['id']);
        $announce->title = $req['title'];
        $announce->content = $req['content'];
        $announce->fid = $req['fid'];
        $announce->zhuangtai = $req['zhuangtai'];
        if ($announce->save()) {
            return ['code'=>200,'msg'=>'修改成功'];
        }
        return ['code'=>400,'msg'=>'修改失败'];
    }



    /**
     * 添加公告
     */
    public function addannounce($req){
        $this->title = $req['title'];
        $this->content = $req['content'];
        $this->fid = $req['fid'];
        $this->addtime = time();
        $this->fengmian = '';
        $this->zhuangtai = $req['zhuangtai'];
        if($this->create()){
              return ['code'=>200,'msg'=>'添加成功'];
        }
        return ['code'=>400,'msg'=>'添加失败'];
    }
}